package pl.android.kalkulator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class KalkulatornaukowyActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
    
    public void buttonClick(View view){
    	EditText numA  = (EditText)findViewById(R.id.editText1);
    	EditText numB  = (EditText)findViewById(R.id.editText2);
    	EditText wynik = (EditText)findViewById(R.id.editText3);
    	float num1 = Float.parseFloat(numA.getText().toString());
    	float num2 = Float.parseFloat(numB.getText().toString());
    	float eq = 0;

    	switch(view.getId()){
    		case R.id.button1:
    			eq = num1 - num2;
    			break;
    		case R.id.button2:
    			eq = num1 + num2;
    			break;
    		case R.id.button3:
    			eq = num1 * num2;
    			break;
    		case R.id.button4:
    			eq = num1 / num2;
    			break;
    		case R.id.button5: //sin
    			eq=(float) Math.sin(eq);
    			break;
    		case R.id.button6: //cos
    			eq=(float) Math.cos(eq);
    			break;
    		case R.id.button7: //tg
    			eq=(float) Math.tan(eq);
    			break;
    		case R.id.button8: //log
    			eq=(float) Math.log(eq);
    			break;
    		case R.id.button9: //mod
    			eq = num1 % num2;
    			break;
    		case R.id.button10: //E
    			num1=(float) Math.E;
    			numA.setText(String.format("%f", num1));
    			break;
    		case R.id.button11: //PI
    			num1=(float) Math.PI;
    			numA.setText(String.format("%f", num1));
    			break;
    		case R.id.button12: //pow
    			eq = (float) Math.pow(num1, num2);
    			break;
    		case R.id.button13: //!
    			eq=factorial(eq);
    			break;
    		case R.id.button14: //%
    			eq=(eq/100);
    			break;
    	}

    	wynik.setText(String.format("%f", eq));
    }

	private float factorial(float num1) {
		float result=1;
		for(int i=2; i<num1; i++) result*=i;
		return result;
	}
}
